package com.zwr.sheller.customer.Service.Impl;

import com.zwr.sheller.core.common.WQQBusinessException;
import com.zwr.sheller.core.common.WQQExceptionCode;
import com.zwr.sheller.customer.Service.GoodsService;
import com.zwr.sheller.customer.mapper.GoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author: 18032
 * @Date: 10:28 2022/1/14
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired(required = false)
    GoodsMapper goodsMapper;

    @Override
    public List<Map<String, Object>> selectGoodsBySortAndPrice(Long itemId, Long rankId, Boolean numOrder, Boolean priceOrder, Long choose, Double lowPrice, Double highPrice) throws WQQBusinessException {
        //1. 判断注入参数是否合理
        if (rankId != 1L && rankId != 2L && rankId != 3L) {
            throw new WQQBusinessException(WQQExceptionCode.WRONG_RANKID);
        }
        if (choose != null && choose != 1L && choose != 2L && choose != 3L ){
            throw  new WQQBusinessException(WQQExceptionCode.WRONG_CHOOSE);
        }
        if (lowPrice == null){
            lowPrice = 0.0;
        }
        if (highPrice == null){
            highPrice = Double.MAX_VALUE;
        }
        if ( lowPrice >= highPrice){
            throw  new WQQBusinessException(WQQExceptionCode.WRONG_PRICE);
        }
        if (priceOrder == null){
            priceOrder = true;
        }


        //查询
        List<Map<String, Object>> goodsList = goodsMapper.selectGoodsByType(itemId, rankId, priceOrder, choose, lowPrice, highPrice);
        if (goodsList == null){
            throw  new WQQBusinessException(WQQExceptionCode.NO_GOOD);
        }

        return goodsList;
    }
}
